package org.goobs.applications;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.goobs.internet.Browser;
import org.goobs.internet.Hyperlink;
import org.goobs.internet.Browser.BrowserException;
import org.goobs.utils.Util;

//http://www.naturalproductsinsider.com/guide/buyersguide.asp

public class NaturalProductsCompanyExtractor {

	private static final Browser browser = new Browser();
	
	private static FileWriter writer;

	private static void extractInformation(String url){
		System.out.println(url);
		browser.pointTo(url);

		String name = " "; 
		try {
			name = Browser.extractText( browser.findFirstHtmlBetween("<p><h3>", "</h3></p>", false) );
		} catch (BrowserException e) {
		}
		String address = " ";
		try {
			address = Browser.extractText( browser.findFirstHtmlBetween("<td class=\"buyers_guide\" width=\"50%\" valign=\"top\"><i>", "<br></i>", false) );
		} catch (BrowserException e) {
		}
		String phone = " ";
		try {
			phone = Browser.extractText( browser.findFirstHtmlBetween("Phone: ", "<br>Fax", false) );
		} catch (BrowserException e) {
		}
		String fax = " ";
		try {
			fax = Browser.extractText( browser.findFirstHtmlBetween("Fax: ", "<br>", false) );
		} catch (BrowserException e) {
		}
		String email  = " ";
		try {
			email = Browser.extractText( browser.findFirstHtmlBetween("Email: <a href=\"mailto:", "\">", false) );
		} catch (BrowserException e) {
		}
		String contact = " ";
		try {
			contact = Browser.extractText( browser.findFirstHtmlBetween("Contact: </b> ", "<br>", false) );
		} catch (BrowserException e) {
		}
		String description = " ";
		try {
			description = Browser.extractText( browser.findFirstHtmlBetween("<p class=\"buyers_guide_description\">\n", "\n						</p>", false) ).substring(1);
		} catch (BrowserException e) {
		}
		System.out.println(name + "\n" + address + "\n" + phone + "\n" + fax + "\n" + email + "\n" + contact + "\n" + description + "\n\n\n");
		try {
			writer.write(name + "~" + email + "~" + contact + "~" + phone + "~" + fax + "~" + description + "~" + url + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		System.exit(1);
	}

	private static boolean followLinks(char letter){
		try {
			browser.pointTo("http://www.naturalproductsinsider.com/guide/buyersguideSearch.asp?al=" + letter);
		} catch (BrowserException e) {
			return false;
		}

		//--Get links
		List <Hyperlink> links = browser.getLinks("<!--LEFT COLUMN-->", "<!-- buyers email service -->");
		//--Extract information
		Iterator <Hyperlink> iter = links.iterator();
		//(first two are not company links)
		iter.next();
		iter.next();
		//(parse company links)
		while(iter.hasNext()){
//			System.out.println(iter.next().getText());
			extractInformation(iter.next().getURL());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return true;
	}

	public static void main(String[] args){
		File f = new File("/home/gabor/out.csv");
		try {
			f.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			writer = new FileWriter(f);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		for( char letter : Util.ALPHABET_UPPER ){
			if(!followLinks(letter)){
				System.out.println("ERROR: Could not parse menu for '" + letter + "'");
			}
		}
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
